/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.ac.cput.pretorius.programs.program01.impl;

import java.util.Iterator;
import za.ac.cput.pretorius.programs.program01.BinaryConverter;

/**
 *
 * @author griddy
 */
public class BinaryConverterImpl implements BinaryConverter{
    
    
    

    @Override
    public int binToDec(String binNumber) {
        
        int decimalValue = 0;
        int bitPosition = 1;
        int bit = 0;
        
        try
        {
            
        
            for (int i = binNumber.length(); i!=0 ; i--)
            {
                bit = Integer.parseInt(binNumber.charAt(i-1)+"");

                switch(bit)
                {
                    case 1 :    decimalValue += bitPosition;
                    case 0 :
                                bitPosition = bitPosition * 2;
                        break;
                    default:
                        throw new Exception();
                        

                }

            }
        }catch(Exception e)
        {
            
          decimalValue = -1;  
        }
        return decimalValue;
    }
    
    
    
}
